package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import com.sky.utils.UserThreadLocalUtil;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * PROJECT_NAME: sky-take-out
 * NAME: ShoppingCartServiceImpl
 *
 * @AUTHOR: zhang
 * DATE: 2023/11/30
 * DESCRIPTION :
 */
@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private DishFlavorsMapper dishFlavorsMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO 要添加的菜品(口味)/套餐
     */
    @Override
    public void insertShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart newShoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, newShoppingCart);
        //判断要添加的菜品/套餐在购物车中是否存在
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        //根据dishId/setmealId查询购物车表中是否存在(返回集合，提高复用性)
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectShoppingCart(newShoppingCart);
        //因为前端传递传递的dto中只有菜品id和套餐id，所以结果只有一条数据
        if (shoppingCartList!=null && shoppingCartList.size() == 1) {
            ShoppingCart shoppingCart = shoppingCartList.get(0);
            //存在，则更新数量
            int number = 0;
            number = shoppingCart.getNumber()+1;
            shoppingCart.setNumber(number);
            shoppingCartMapper.update(shoppingCart);
        }else {
            //不存在，则新增
            //判断要新增的是菜品还是套餐
            if (shoppingCartDTO.getDishId() != null) {
                //说明要添加的是菜品，是菜品则要查询菜品包含的口味
                //根据dishId查询菜品信息表
                DishVO dishVO = dishMapper.selectDishById(dishId);
                //补齐字段 name image userId number amount create_time
                newShoppingCart.setName(dishVO.getName());
                newShoppingCart.setImage(dishVO.getImage());
                newShoppingCart.setUserId(BaseContext.getCurrentId());
                newShoppingCart.setSetmealId(null);
                newShoppingCart.setNumber(1);
                newShoppingCart.setAmount(dishVO.getPrice());
                newShoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insertShoppingCart(newShoppingCart);
            } else {
                //是套餐，则直接新增
                //根据套餐id查询套餐信息
                Setmeal setmeal = setmealMapper.selectSetmeal(setmealId);
                //补齐字段 name image userId dish_id dish_flavor number amount create_time
                newShoppingCart.setName(setmeal.getName());
                newShoppingCart.setImage(setmeal.getImage());
                newShoppingCart.setUserId(BaseContext.getCurrentId());
                newShoppingCart.setDishId(null);
                newShoppingCart.setDishFlavor(null);
                newShoppingCart.setNumber(1);
                newShoppingCart.setAmount(setmeal.getPrice());
                newShoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insertShoppingCart(newShoppingCart);
            }
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public Result<List<ShoppingCart>> selectShoppingCart() {
        log.info("【获取到的用户id】:{}",BaseContext.getCurrentId());
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectShoppingCart(shoppingCart);
        return Result.success(shoppingCartList);
    }

    /**
     * 删减购物车中的商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //根据id查询要删除的套餐/菜品
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectShoppingCart(shoppingCart);
        if (shoppingCartList!=null && shoppingCartList.size() ==1){
            ShoppingCart cart = shoppingCartList.get(0);
            //获取菜品/套餐的数量
            int number = 0;
            number = cart.getNumber();
            if (number>1){
                //获取菜品/套餐的数量-1
                number = number-1;
                cart.setNumber(number);
                shoppingCartMapper.update(cart);
            }else{
                //如果数量为1直接删除菜品/套餐
                shoppingCartMapper.delete(cart);
            }

        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clearShoppingCart() {
        //获取要清空购物车的用户id
        Long userId = BaseContext.getCurrentId();
        //封装购物车对象，用于清空购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        //清空
        shoppingCartMapper.delete(shoppingCart);

    }
}
